import pandas as pd
import numpy as np
import time

# 多sheet情境的内容合并
# # 方法一：读取内容矩阵后拼接（较麻烦）
# t1 = time.time()
# df = pd.ExcelFile('for_test.xlsx')
# # 查看所有sheet名称
# # print(df.sheet_names)
# # 先定义初始0矩阵，后面再删
# mat = np.zeros([1, 14])
# i = 0
# for sheet in df.sheet_names:
#     each_page = pd.read_excel('for_test.xlsx', sheet_name=sheet)
#     each_mat = each_page.values
#     mat = np.r_[mat, each_mat]  # 一般多页sheet内容列数不变，采用行合并
#     i += 1
#     if i == 10:  # 简单读到前几页查看效果即可
#         break
# mat = np.delete(mat, 0, axis=0)  # 将初始0矩阵删除
# print(mat, mat.shape)
# print(time.time()-t1)

# 方法二：直接进行表拼接（简便）
df = pd.ExcelFile('test2.xlsx')
page_ls = []
for sheet in df.sheet_names:
    each_page = pd.read_excel('test2.xlsx', sheet_name=sheet)
    page_ls.append(each_page)
mat = pd.concat(page_ls, axis=0, ignore_index=True)  # ignore_index表示忽略原索引
print(mat.head(10), mat.shape)

# # 线程
# from concurrent.futures import ProcessPoolExecutor
# import time
#
# def page(sheet):
#     each_page = pd.read_excel('for_test.xlsx', sheet_name=sheet)
#     return each_page
#
# if __name__ == "__main__":
#     t1 = time.time()
#     df = pd.ExcelFile('for_test.xlsx')
#     ls = df.sheet_names[:10]  # 需要读取的工作表切片
#     page_ls = []  # 存放每个表，用于后续合成
#     pool = ProcessPoolExecutor(max_workers=5)  # 貌似还是得靠多进程提速
#     t_lst = []
#     for sheet in ls:
#         t = pool.submit(page, sheet)
#         t_lst.append(t)
#     pool.shutdown()
#     for ti in t_lst:
#         page_ls.append(ti.result())
#     mat = pd.concat(page_ls, axis=0, ignore_index=True)  # ignore_index表示忽略原索引
#     print(mat, mat.shape)
#     print(time.time()-t1)

# from mypack import Mypool
# from time import time
# if __name__ == '__main__':  # 无此语句多进程循环会调用报错
#     t1 = time()
#     li = [f'第{i+1}页' for i in range(10)]
#     pool = Mypool.MyProcessPool(5, page, li)
#     print(pool.go())
#     print(time()-t1)